const { Op } = require("sequelize");
const userModel = appRequire('models/user');
const artPPTModel = appRequire('models/artPPT');
const orderModel = appRequire('models/order');

module.exports = {
    moneyList,
    adminMoneyList,
};


async function adminMoneyList(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let params = {
        status: 1,
        payMoney: { [Op.gt]: 0 }
    }
    if (postData.time) {
        let times = postData.time.split("-")
        let startTime = postData.time + "-" + "01 00:00:00"
        let endTime = postData.time + "-" + "31 23:59:59"
        params.createdAt = {
            [Op.gte]: startTime,
            [Op.lte]: endTime
        }
    }
    params.type = [1, 2]
    let result1 = await orderModel.order.findAndCountAll({
        where: params,
        order: [['createdAt', 'DESC']],
        limit: postData.pageSize,
        offset: postData.pageSize * (postData.pageNum - 1),
        include: [{
            model: artPPTModel,
            as: 'ppt',
            attributes: ['title'],
        }],
    })

    let result2 = await orderModel.order.sum('payMoney', {
        where: params,
    })

    result1 = Common.clone(result1)
    for (let item of result1.rows) {
        item.pptTitle = "暂无"
        if (item.ppt && item.ppt.title) item.pptTitle = item.ppt.title
    }
    returnData.totalMoney = Common.clone(result2)
    returnData.maxCount = result1.count
    returnData.maxPage = Math.ceil(result1.count / postData.pageSize)
    returnData.data = result1.rows
    return request.json(returnData)
}

async function moneyList(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['uid'], request);
    if (checkResult == false) return;

    let params = {
        status: 1,
        payMoney: { [Op.gt]: 0 }
    }
    if (postData.time) {
        let times = postData.time.split("-")
        let startTime = postData.time + "-" + "01 00:00:00"
        let endTime = postData.time + "-" + "31 23:59:59"
        params.createdAt = {
            [Op.gte]: startTime,
            [Op.lte]: endTime
        }
    }
    if (postData.uid) params.sellerId = postData.uid
    if (postData.type) params.type = postData.type
    let result1 = await orderModel.order.findAndCountAll({
        where: params,
        order: [['createdAt', 'DESC']],
        limit: postData.pageSize,
        offset: postData.pageSize * (postData.pageNum - 1),
        include: [{
            model: artPPTModel,
            as: 'ppt',
            attributes: ['title'],
        }],
    })

    let result2 = await orderModel.order.sum('payMoney', {
        where: params,
    })

    result1 = Common.clone(result1)
    for (let item of result1.rows) {
        item.pptTitle = "暂无"
        if (item.ppt && item.ppt.title) item.pptTitle = item.ppt.title
    }

    returnData.totalMoney = Common.clone(result2)
    returnData.maxCount = result1.count
    returnData.maxPage = Math.ceil(result1.count / postData.pageSize)
    returnData.data = result1.rows
    return request.json(returnData)
}

